package com.kly.cashmall.utils.data;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author
 * @date 2017/8/16
 */
public class TextUtil {

  /**
   * 判断空字符串
   */
  public static boolean isEmpty(CharSequence charSequence) {
    return TextUtils.isEmpty(charSequence) || charSequence.toString().trim().length() == 0 || "null"
        .equals(charSequence) || "undefined".equals(charSequence);
  }

  /**
   * 过滤null
   */
  public static String avoidNull(CharSequence charSequence) {
    if (TextUtil.isEmpty(charSequence)) {
      return "";
    }
    return charSequence.toString();
  }

  public static CharSequence ifNullDefault(CharSequence charSequence, CharSequence defaultValue) {
    return TextUtil.isEmpty(charSequence) ? defaultValue : charSequence;
  }

  /**
   * 截取字符串
   */
  public static String substring(String text, int start) {
    if (TextUtil.isEmpty(text)) {
      return "";
    }
    if (start < 0) {
      start = 0;
    }
    return text.length() > start ? text.substring(start) : "";
  }

  /**
   * 截取字符串
   */
  public static String substring(String text, int start, int end) {
    if (TextUtil.isEmpty(text)) {
      return "";
    }
    if (start < 0) {
      start = 0;
    }
    if (end > text.length()) {
      end = text.length();
    }
    if (end <= start) {
      return "";
    }
    return text.substring(start, end);
  }

  public static String decodeURL(String string) {
    if (isEmpty(string)) {
      return "";
    } else {
      try {
        String decodeStr = URLDecoder.decode(string, "UTF-8");
        return isEmpty(decodeStr) ? string : decodeStr;
      } catch (UnsupportedEncodingException var2) {
        var2.printStackTrace();
        return string;
      }
    }
  }
}
